<script setup>
  import { ref } from 'vue'
  import { orderListApi } from '@/services/medicine.js'
  const feedTabs = ref([
    { label: '全部', rendered: true, status: 13 },
    { label: '待支付', rendered: false, status: 10 },
    { label: '待发货', rendered: false, status: 11 },
    { label: '待收货', rendered: false, status: 12 },
    { label: '已完成', rendered: false, status: 14 }
  ])
  // 加载状态
  const loading = ref(true)
  // 列表是否为空
  const isEmpty = ref(false)
  // 订单列表
  const orderList = ref([])
  // 标签页切换
  function onTabClick({ index, status }) {
    console.log(index, status)
    // 获取订单列表
    getOrderList(status)
  }
  const getOrderList = async (status) => {
    // 显示加载状态
    loading.value = true
    // 列表是否为空
    isEmpty.value = false
    const res = await orderListApi(status)
    orderList.value = res.data.data.rows
    // 关闭加载状态
    loading.value = false
    // 列表是是否为空
    isEmpty.value = orderList.value?.length === 0
  }
  getOrderList()
</script>

<template>
  <view class="medicine-page">
    <view class="order-status-tabs">
      <custom-tabs @click="onTabClick" :list="feedTabs"></custom-tabs>
    </view>
    <scroll-view
      refresher-enabled
      refresher-background="#f6f6f6"
      class="uni-scroll-view"
      scroll-y
    >
      <view class="order-list">
        <view
          v-for="order in orderList"
          :key="order.id"
          class="order-list-item"
        >
          <view class="order-header">
            <text class="number">订单编号: {{ order.id }}</text>
            <text class="status">待支付</text>
          </view>
          <navigator
            :url="`/subpkg_medicine/order_detail/index?orderId=${order.id}`"
            hover-class="none"
            class="order-body"
          >
            <view
              v-for="medicine in order.medicines"
              :key="medicine.id"
              class="medicine"
            >
              <image
                class="medicine-cover"
                :src="medicine.avatar"
                mode="aspectFill"
              />
              <view class="medicine-info">
                <text class="name">{{ medicine.name }}</text>
                <text
                  :class="{ symbol: medicine.prescriptionFlag === 1 }"
                  class="unit"
                >
                  {{ medicine.specs }}
                </text>
                <text class="price">¥{{ medicine.amount }}</text>
              </view>
              <view class="quantity">x{{ medicine.quantity }}</view>
            </view>
          </navigator>
          <view class="order-footer">
            <view class="total">
              共2件商品 合计：¥{{ order.actualPayment }}（运费¥0.00）
            </view>
            <view class="buttons">
              <button class="uni-button minor">取消订单</button>
              <button class="uni-button">继续支付</button>
            </view>
          </view>
        </view>

        <!-- 列表数据为空 -->
        <view v-if="isEmpty" class="empty-tips">没有更多数据了</view>

        <!-- 加载状态 -->
        <uni-load-more
          v-show="loading"
          status="loading"
          color="#C3C3C5"
          :icon-size="16"
          :content-text="{
            contentdown: '上拉显示更多',
            contentrefresh: '数据正在加载中',
            contentnomore: '没有更多数据了'
          }"
        />
      </view>
    </scroll-view>
  </view>
</template>

<style lang="scss">
  .medicine-page {
    height: 100vh;
    /* #ifdef H5 */
    height: calc(100vh - 44px);
    /* #endif */
    background-color: #f6f6f6;
    box-sizing: border-box;
    padding-bottom: calc(env(safe-area-inset-bottom) + 20rpx);

    display: flex;
    flex-direction: column;
  }

  .order-status-tabs {
    padding: 20rpx 30rpx 0;
    background-color: #fff;

    :deep(.custom-tabs) {
      justify-content: space-evenly;
    }
  }

  .uni-scroll-view {
    flex: 1;
    overflow: hidden;
    padding-top: 15rpx;
  }

  .order-list {
    padding: 15rpx 30rpx 30rpx;
  }

  .order-list-item {
    // height: 715rpx;
    padding: 0 30rpx;
    margin-bottom: 30rpx;
    border-radius: 10rpx;
    background-color: #fff;

    .order-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 100rpx;
      padding: 0 30rpx;
      margin: 0 -30rpx;
      font-size: 26rpx;
      border-bottom: 1rpx solid #f5f5f5;

      .number {
        color: #848484;
      }

      .status {
        color: #f2994a;
      }
    }

    .medicine {
      padding: 30rpx 0;
      border-bottom: 1rpx solid #f5f5f5;
      position: relative;

      .medicine-cover {
        width: 180rpx;
        height: 160rpx;
        float: left;
      }

      .medicine-info {
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        height: 160rpx;
        padding-left: 20rpx;

        .name {
          font-size: 30rpx;
          font-weight: 500;
          color: #121826;
        }

        .unit {
          color: #979797;
          font-size: 28rpx;
          display: flex;
          align-items: center;

          @include text-overflow(1);

          &.symbol::before {
            content: '处方药';
            line-height: 1;
            padding: 8rpx 10rpx;
            margin-right: 10rpx;
            border-radius: 4rpx;
            font-size: 24rpx;
            color: #fff;
            background-color: #16c2a3;
          }
        }

        .price {
          color: #eb5757;
          font-size: 32rpx;
        }
      }

      .quantity {
        top: 30rpx;
        right: 0;
        position: absolute;

        font-size: 30rpx;
        color: #121826;
      }
    }

    .order-footer {
      padding: 30rpx 0;

      .total {
        text-align: right;
        font-size: 24rpx;
        color: #3c3e42;
      }

      .buttons {
        display: flex;
        justify-content: flex-end;
        padding-top: 20rpx;
      }

      .uni-button {
        height: 60rpx;
        line-height: 60rpx;
        margin: 20rpx 0 0 30rpx;
        font-size: 26rpx;
        color: #16c2a3;
        border: 1rpx solid #16c2a3;
        background-color: #fff;

        &.minor {
          color: #3c3e42;
          border-color: #ededed;
          background-color: #f6f6f6;
        }
      }
    }
  }
</style>
